<?php


namespace app\api\validate;


use think\Validate;

/**
 * 商品模块校验类
 */
class ShopValidate extends Validate
{

    // 校验规则
    protected $rule = [
        'factorId' => 'require|number',
        'page' => 'require|number',
        'limit' => 'require|number|between:2,100',
        'factorTypeId' => 'require|array|factorTypeId',
        'typeId' => 'require|number|typeId',
        'content' => 'require|chsDash|length:1,100',
        'id' => 'require|number',
    ];
    // 报错提示信息
    protected $message = [
        'factorId.require' => '请输入factorId',
        'factorId.number' => 'factorId请输入整数',
        'page.require' => '请输入page参数',
        'page.number' => 'page请输入整数',
        'limit.require' => '请输入limit参数',
        'limit.number' => 'limit请输入整数',
        'limit.between' => 'limit的范围在2-100之间',
        'factorTypeId.require' => '请输入factorTypeId参数',
        'factorTypeId.number' => 'factorTypeId请输入整数',
        'content.require' => '请输入content',
        'content.chsDash' => 'content字符只能是汉字、字母、数字和下划线_及破折号-',
        'content.length' => 'content长度在1-100字符之间',
        'typeId.require' => '请输入typeId',
        'typeId.number' => 'typeId请输入整数',
        'id.require' => '请输入id',
        'id.number' => 'id请输入整数',
    ];
    // 校验场景
    protected $scene = [
        'collection' => ['factorId'],
        'factor' => ['page', 'limit', 'factorTypeId'],
        'template' => ['page', 'limit'],
        'search' => ['page', 'limit', 'typeId', 'content'],
        'info' => ['id'],
    ];

    /**
     * 校验因子的查询分类
     */
    protected function factorTypeId($value, $rule, $data)
    {
        $type = [1, 2];
        return !empty($value) && empty(array_diff($value, $type)) ?: '请输入正确的核算边界';
    }

    protected function typeId($value, $rule, $data)
    {
        $type = [1, 2];
        return in_array($value, $type) ?: '请输入正确的类别ID';
    }

}